
package com.edgar.web;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.RelativeLayout;
import com.edgar.main.R;

public class GetXML extends Activity {
    private WebView mWebView;
    private Handler mHandler = new Handler();
    File file;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.getxml);
       
        file = new File(
		        Environment.getExternalStorageDirectory().toString()+"/web/person.xml");
        
        ( (TextView) this.findViewById(R.id.getxml_tv)).setText(getxml(file));
    }


	private String  getxml(File file) {
		StringBuffer SB = new StringBuffer();
		try {
			DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = doc.newDocumentBuilder();
			Document document = builder.parse(file);
			SB.append("Root elemnet of doc is");
			SB.append(document.getDocumentElement().getNodeName());
			
			NodeList personNodes = document.getElementsByTagName("person");
			int num = personNodes.getLength();
			SB.append("/r/n total num is");
			SB.append(num);
			
			for(int i = 0;i<num;i++){
				Node first = personNodes.item(i);
				if(first.getNodeType() == Node.ELEMENT_NODE){
					Element firstEle = (Element)first;
					
					NodeList firstNodes = firstEle.getElementsByTagName("first");
					Element firstnameEle = (Element)firstNodes.item(0);
					NodeList firstnameNodes = firstnameEle.getChildNodes();
					SB.append("/r/n first name is");
					SB.append(firstnameNodes.item(0).getNodeValue().trim());
					
					NodeList lastNodes = firstEle.getElementsByTagName("last");
					Element lastnameEle = (Element)lastNodes.item(0);
					NodeList lastnameNodes = lastnameEle.getChildNodes();
					SB.append("/r/n last name is");
					SB.append(lastnameNodes.item(0).getNodeValue().trim());
					
					NodeList ageNodes = firstEle.getElementsByTagName("age");
					Element ageEle = (Element)ageNodes.item(0);
					NodeList agevalueNodes = ageEle.getChildNodes();
					SB.append("/r/n last name is");
					SB.append(agevalueNodes.item(0).getNodeValue().trim());
	
				}
				
			}
			
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SB.toString();
	}
	
	public static List<Person> getPersons(InputStream inStream) throws Exception{
		List<Person> persons = new ArrayList<Person>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(inStream);
		Element root = document.getDocumentElement();
		NodeList personNodes = root.getElementsByTagName("person");
		for(int i=0; i < personNodes.getLength() ; i++){
			Element personElement = (Element)personNodes.item(i);
			int id = new Integer(personElement.getAttribute("id"));
			Person person = null;// = new Person();
			person.setId(id);
			NodeList childNodes = personElement.getChildNodes();
			for(int y=0; y < childNodes.getLength() ; y++){
				if(childNodes.item(y).getNodeType()==Node.ELEMENT_NODE){
					if("name".equals(childNodes.item(y).getNodeName())){
						String name = childNodes.item(y).getFirstChild().getNodeValue();
						person.setName(name);
					}else if("age".equals(childNodes.item(y).getNodeName())){
						String age = childNodes.item(y).getFirstChild().getNodeValue();
						person.setAge(new Short(age));
					}
				}
			}
			persons.add(person);
		}
		inStream.close();
		return persons;
	}
	
    public class Person{
    	String first;
    	String last;
    	int age;
    	public Person(){}
		public void setName(String name) {
			// TODO Auto-generated method stub
			
		}
		public void setId(int id) {
			// TODO Auto-generated method stub
			
		}
		public void setAge(Short short1) {
			// TODO Auto-generated method stub
			
		}
    }
}